package com.ehotting.mqbd.request.config;

import com.ehotting.mqbd.api.bean.MqApplicationContext;
import com.ehotting.mqbd.api.enums.DirectionEnum;
import com.ehotting.mqbd.api.enums.TopicEnum;
import com.ehotting.mqbd.core.service.MQService;
import com.ehotting.mqbd.core.logger.MqMsgLogger;
import com.ehotting.mqbd.core.manager.ModuleManager;
import com.ehotting.mqbd.core.manager.SolverAllocator;
import com.ehotting.mqbd.core.call.CallerAble;
import com.ehotting.mqbd.core.config.MqbdCorePropertyConfig;
import com.ehotting.mqbd.core.helper.MQStringHelper;
import com.ehotting.mqbd.core.mq.consumer.MessageConsumer;
import com.ehotting.mqbd.core.mq.consumer.action.ConsumerRequestAction;
import com.ehotting.mqbd.core.manager.SolverStateManager;
import com.ehotting.mqbd.request.call.HttpCaller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

@Configuration
public class ConsumerActionRequestConfig {

    @Autowired
    private MqApplicationContext mqApplicationContext;

    @Bean(name="httpCaller")
    public HttpCaller httpCaller(MQService mqService, SolverStateManager solverStateManager){
        HttpCaller caller = new HttpCaller() {
            @Override
            public String getModuleName() {
                return "项目";
            }
        };
        caller.setMqService(mqService);
        caller.setSolverStateManager(solverStateManager);
        return caller;
    }

    @Bean(name="consumerRequestBlockAction")
    public ConsumerRequestAction consumerRequestBlockAction(CallerAble httpCaller,
                                                            SolverAllocator solverAllocator,
                                                            MqApplicationContext mqApplicationContext, ModuleManager moduleManager, MqMsgLogger mqMsgLogger){
        ConsumerRequestAction action = new ConsumerRequestAction(solverAllocator,
                httpCaller,
                 mqApplicationContext,
                TopicEnum.BLOCK,
                DirectionEnum.REQUEST,
                null,
                moduleManager,mqMsgLogger);

        return action;
    }

    @Bean(name="consumerRequestConcurrencyAction")
    public ConsumerRequestAction consumerRequestConcurrencyAction(CallerAble httpCaller,
                                                                  SolverAllocator solverAllocator,
                                                                  MqApplicationContext mqApplicationContext, ModuleManager moduleManager, MqMsgLogger mqMsgLogger){
        ConsumerRequestAction action = new ConsumerRequestAction(solverAllocator,
                httpCaller,
                mqApplicationContext,
                TopicEnum.CONCURRENCY,
                DirectionEnum.REQUEST,
                null,
                moduleManager,mqMsgLogger);

        return action;
    }

    @DependsOn("mqbdCorePropertyConfig")
    @Bean(initMethod = "start",destroyMethod = "shutdown")
    public MessageConsumer requestConcurrencyMessageConsumer(MqbdCorePropertyConfig mqbdCorePropertyConfig,
                                                             ConsumerRequestAction consumerRequestConcurrencyAction){
        MessageConsumer messageConsumer = new MessageConsumer(
                mqbdCorePropertyConfig.getConsumer(),
                MQStringHelper.createConsumerGroupAtConsumerApp(
                        TopicEnum.CONCURRENCY,
                        mqApplicationContext,DirectionEnum.REQUEST),
                consumerRequestConcurrencyAction);

        return messageConsumer;
    }

    @DependsOn("mqbdCorePropertyConfig")
    @Bean(initMethod = "start",destroyMethod = "shutdown")
    public MessageConsumer requestBlockMessageConsumer(MqbdCorePropertyConfig mqbdCorePropertyConfig,
                                                       ConsumerRequestAction consumerRequestBlockAction){
        MessageConsumer.MessageConsumerParam consumerParam = mqbdCorePropertyConfig.getConsumer().clone();
        consumerParam.setConsumeThreadMin(1);
        consumerParam.setConsumeMessageBatchMaxSize(1);
        MessageConsumer messageConsumer = new MessageConsumer(
                consumerParam,
                MQStringHelper.createConsumerGroupAtConsumerApp(TopicEnum.BLOCK , mqApplicationContext , DirectionEnum.REQUEST),
                consumerRequestBlockAction);

        return messageConsumer;
    }

}